
package common;
//服务端返回到前端的高复用响应对象
public class ServerResponse<T>{
	private int status;//返回到前端的转台吗
	private String msg;//当status不为0时封装错误信息
	private T data;//返回到前端的数据
	private int count;//数量（用于分页）
	private ServerResponse() {
		
	}
	private ServerResponse(int status) {
		this.status = status;
	}
	private ServerResponse(int status, T data) {//status=0
		this.status = status;
		this.data = data;
	}
	private ServerResponse(int status, String msg) {//status非0，错误信息
		this.status = status;
		this.msg = msg;
	}
	private ServerResponse(int status, T data, String msg) {//status=0，同时提供错误信息
		this.status = status;
		this.msg = msg;
		this.data = data;
	}
	private ServerResponse(int status, int count, T data, String msg) {//用于分页
		this.status = status;
		this.count=count;
		this.msg = msg;
		this.data = data;
	}

	
	
	/**
	 * 调用接口成功时的回调
	 * */
	public static ServerResponse serverResponseBySuccess() {
		return new ServerResponse(ResponseCode.SUCCESS);
	}
	public static <T>ServerResponse serverResponseBySuccess(T data) {
		return new ServerResponse(ResponseCode.SUCCESS,data);
	}
	public static <T>ServerResponse serverResponseBySuccess(T data,String msg) {
		return new ServerResponse(ResponseCode.SUCCESS,data,msg);
	}

	public static <T>ServerResponse serverResponseBySuccess(int count,T data,String msg) {
		return new ServerResponse(ResponseCode.SUCCESS,count,data,msg);
	}
	
	/**
	 * 接口调用失败时的回调
	 * */
	public static ServerResponse serverResponseByError() {
		return new ServerResponse(ResponseCode.ERROR);
	}
	public static ServerResponse serverResponseByError(String msg) {
		return new ServerResponse(ResponseCode.ERROR,msg);
	}
	public static ServerResponse serverResponseByError(int status) {
		return new ServerResponse(status);
	}
	public static ServerResponse serverResponseByError(int status,String msg) {
		return new ServerResponse(status,msg);
	}
	
	//判断接口是否正确返回
	//只需要判断status是否为0
	public boolean isSuccess() {
		return this.status==ResponseCode.SUCCESS;
	}
	
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public String getMsg() {
		return msg;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}

	
	
	
	 
	
}